package com.ado.bazinga.core.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ado.bazinga.core.annotation.AdminLoginToken;
import com.ado.bazinga.core.annotation.ApiDesc;
import com.ado.bazinga.core.annotation.UserLoginToken;
import com.ado.bazinga.core.assemble.OrderAssemble;
import com.ado.bazinga.core.common.http.HttpResult;
import com.ado.bazinga.core.common.params.dto.OrderDto;
import com.ado.bazinga.core.common.params.vo.GoodVO;
import com.ado.bazinga.core.common.params.vo.OrderVO;
import com.ado.bazinga.core.common.params.vo.UserParam;
import com.ado.bazinga.core.dal.entity.Good;
import com.ado.bazinga.core.dal.entity.Order;
import com.ado.bazinga.core.dal.entity.User;
import com.ado.bazinga.core.exception.BazingaException;
import com.ado.bazinga.core.service.GoodService;
import com.ado.bazinga.core.service.OrderService;
import com.ado.bazinga.core.service.UserService;
import com.ado.bazinga.core.utils.IDUtils;

/**
 *@author adonais
 *@date 2020年3月23日
 *@description OrderController.java 订单控制器
**/
@RestController
@RequestMapping(value = "/api/order")
public class OrderController {
	
	@Autowired
	@Qualifier(value = "orderService")
	private OrderService orderService ;
	@Autowired
	@Qualifier(value = "orderAssemble")
	private OrderAssemble orderAssemble ;
	@Autowired
	@Qualifier(value = "goodService")
	private GoodService goodService ;
	@Autowired
	@Qualifier(value = "userService")
	public UserService userService ;
	
	@ApiDesc(desc = "订单查询接口")
	@UserLoginToken
	@GetMapping(value = "/getOrders")
	public HttpResult getOrders(String userId) {
		if (StringUtils.isEmpty(userId)) {
			throw new BazingaException("用户id不能为空");
		}
		List<Order> orderList = orderService.getOrders(userId) ;
		if (orderList.size() > 0) {
			List<OrderVO> orderVOs = orderAssemble.assembleBatchToVO(orderList) ;
			return HttpResult.ok(orderVOs);
		}
		return HttpResult.ok("暂无订单信息") ;
	}
	
	@ApiDesc(desc = "按id查询订单")
	@UserLoginToken
	@AdminLoginToken
	@GetMapping(value = "/getOrderById")
	public HttpResult getOrderById(String orderId) {
		if(StringUtils.isEmpty(orderId)) {
			throw new BazingaException("id不能为空") ;
		}
		Order order = orderService.findOrderById(orderId) ;
		if(order == null) {
			return HttpResult.error(421, "订单不存在") ;
		}
		OrderVO orderVO = new OrderVO() ;
		User user = userService.findUserById(order.getUserId()) ;
		UserParam userParam = new UserParam() ;
		userParam.setUsername(user.getUsername());
		orderVO.setUser(userParam);
		Good good = goodService.getGoodById(order.getGoodId()) ;
		GoodVO goodVO = new GoodVO() ;
		goodVO.setName(good.getName());
		orderVO.setGood(goodVO);
		orderVO.setOrderNo(order.getOrderNo());
		orderVO.setAmount(order.getAmount().toString());
		return HttpResult.ok(orderVO) ;
	}
	
	@ApiDesc(desc = "创建订单接口")
	@UserLoginToken
	@PostMapping(value = "/createOrder")
	public HttpResult createOrder(@RequestBody OrderDto orderDto) {
		if (StringUtils.isEmpty(orderDto.getUserId())) {
			throw new BazingaException("用户id不能为空");
		}
		if (StringUtils.isEmpty(orderDto.getGoodId())) {
			throw new BazingaException("商品id不能为空");
		}
		if (StringUtils.isEmpty(orderDto.getAmount())) {
			throw new BazingaException("金额不能为空");
		}
		Order order = new Order() ;
		Good good = goodService.getGoodById(Integer.valueOf(orderDto.getGoodId())) ;
		good.setStock(good.getStock() - order.getAmount());
		good.setSold(good.getSold() + order.getAmount()) ;
		boolean updateGoodFlag = goodService.updateGood(good) ;
		boolean flag = orderService.createOrder(assembleDtoToEnt(order, orderDto));
		if (flag && updateGoodFlag) {
			return HttpResult.ok("创建订单成功");
		}
		return HttpResult.error("创建订单失败");
	}
	
	@ApiDesc(desc = "删除订单接口")
	@UserLoginToken
	@PostMapping(value = "/deleteOrder")
	public HttpResult deleteOrder(String orderId) {
		if (StringUtils.isEmpty(orderId)) {
			throw new BazingaException("订单id不能为空") ;
		}
		boolean flag = orderService.deleteOrder(Integer.valueOf(orderId)) ;
		if (flag) {
			return HttpResult.ok("删除订单成功");
		}
		return HttpResult.error("删除订单失败");
	}
	
	@ApiDesc(desc = "查询所有订单接口")
	@AdminLoginToken
	@GetMapping(value = "/getAllOrders")
	public HttpResult getAllOrders() {
		List<Order> orders = orderService.getAllOrders() ;
		if (orders.size() <= 0) {
			return HttpResult.error(421, "暂无订单信息") ;
		}
		List<OrderVO> orderVOs = orderAssemble.assembleBatchToVO(orders) ;
		return HttpResult.ok(orderVOs); 
	}
	
	/**
	 *  dto 转实体
	 * @param order
	 * @param orderDto
	 * @return
	 */
	public Order assembleDtoToEnt(Order order , OrderDto orderDto) {
		order.setAmount(Integer.valueOf(orderDto.getAmount()));
		order.setGoodId(Integer.valueOf(orderDto.getGoodId()));
		order.setUserId(Integer.valueOf(orderDto.getUserId()));
		order.setOrderNo(IDUtils.generatorId());
		return order ;
	}

}
